package example1;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ShowBinary extends JFrame{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public ShowBinary(){
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setLayout(null);
        int width = 420;
        int height = 160;
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        int x = (d.width - width) / 2;
        int y = (d.height - height) / 2;
        this.setLocation(x, y);
        this.setSize(width, height);
        this.setTitle("转换二进制文件");
        this.setResizable(false);
        JLabel label = new JLabel("二进制文件:");
        this.add(label);
        label.setBounds(25, 10, 80, 30);
        final JTextField text = new JTextField();
        this.add(text);
        text.setBounds(100, 10, 200, 25);
        text.setEditable(false);
        final JFileChooser file = new JFileChooser();
        file.setFileSelectionMode(JFileChooser.FILES_ONLY);
        this.add(file);
        JButton btn = new JButton("选择");
        btn.setBounds(310, 10, 60, 25);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int state = file.showOpenDialog(null);
                if(state!=1){
                    File f = file.getSelectedFile();
                    text.setText(f.getAbsolutePath());
                }
            }
        });
        this.add(btn);
        JLabel label1 = new JLabel("可看二进制:");
        this.add(label1);
        label1.setBounds(25, 50, 80, 25);
        final JTextField text1 = new JTextField();
        text1.setEditable(false);
        this.add(text1);
        text1.setBounds(100, 50, 200, 25);
        final JFileChooser file1 = new JFileChooser();
        file1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        this.add(file1);
        JButton btn1 = new JButton("选择");
        btn1.setBounds(310, 50, 60, 25);
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int state = file1.showOpenDialog(null);
                if(state!=1){
                    File f = file1.getSelectedFile();
                    text1.setText(f.getAbsolutePath());
                }
            }
        });
        this.add(btn1);
        JButton sure = new JButton("生成");
        sure.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String filepath = text.getText();
                if(filepath==null||(filepath=filepath.trim()).length()==0){
                    return;
                }
                int last = filepath.length();
                if(filepath.contains(".")){
                    last = filepath.lastIndexOf(".");
                }
                String name = filepath.substring(filepath.lastIndexOf(File.separator), last);
                String defaultPath = filepath.substring(0, filepath.lastIndexOf(File.separator));
                String path = text1.getText();
                if(path!=null&&path.trim().length()>0){
                    defaultPath = path.trim();
                }
                String targetName = defaultPath + name + ".txt";
                create(filepath, targetName);
            }
        });
        this.add(sure);
        sure.setBounds(100, 90, 60, 30);
        JButton close = new JButton("关闭");
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        this.add(close);
        close.setBounds(200, 90, 60, 30);
        this.setVisible(true);
    }

    private void create(String source, String target){
        FileOutputStream os = null;
        try{
            os = new FileOutputStream(target);
            byte[] bts = Files.readAllBytes(Paths.get(source));
            int i = 1;
            for(byte b:bts){
                int h = 0xf&(b>>>4);
                int l = 0xf&b;
                String s = (Integer.toHexString(h)+Integer.toHexString(l)).toUpperCase()+" ";
                os.write(s.getBytes());
                if(i%16==0){
                    os.write("\n".getBytes());
                }
                i++;
            }
            os.flush();
        }catch (Exception e) {
            // skip
        }finally{
            if(os!=null){
                try{
                    os.close();
                }catch (Exception e) {
                }
            }
        }
    }

    public static void main(String[] args) {
        new ShowBinary();
    }
}
